package com.my12306.user.config;

import com.my12306.user.service.auth.impl.MyUserDetailService;
import com.my12306.user.util.auth.mobile.SmsCodeAuthenticationSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;

/**
 * @Author:苏牧夕
 * @Date:2020/5/21 1:07
 * @Version 1.0
 */
@EnableWebSecurity
@Configuration
@Order(-1)
@EnableOAuth2Client
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private MyUserDetailService myUserDetailService;
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable()
                .and()
                .requestMatchers()
                .antMatchers(HttpMethod.OPTIONS, "/ilife/oauth/token", "/ilife/mobile/token", "/rest/**", "/api/**", "/**")
                .and().formLogin()
                .and().authorizeRequests().antMatchers("/ilife/oauth/token", "/ilife/mobile/token").permitAll()
                .and()

                .authorizeRequests()
                .anyRequest()
                .permitAll()
                //                .authenticated()
                .and()
                .logout().logoutUrl("/user/puser/logout").logoutSuccessHandler(myLogoutSuccessHandler).clearAuthentication(true)
                .and()
                .cors().and()
                .csrf().disable()
        ;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailService)
                .passwordEncoder(bCryptPasswordEncoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/ilife/oauth/**", "/login.html", "/css/**", "/data/**", "/fonts/**", "/img/**", "/js/**");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
